<?php
/**
 * @title 1268. 搜索推荐系统
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * @title 前缀字典树
 * @author start2004
 * @since 2024-01-06 Created
 */
class WordTree {
    public $words = [];
    public $next = null;
    function __construct($words = [], $next = null){
        $this->words = $words;
        $this->next  = $next;
    }
}

class Solution {

    /**
     * @param String[] $products
     * @param String $searchWord
     * @return String[][]
     */
    function suggestedProducts($products, $searchWord) {
        /**
         * @since 2024-01-06 新建类，存储products
         */
        $tree = new WordTree();

        /**
         * @since 2024-01-06 遍历products数组
         */
        sort($products);
        foreach ($products as $product){
            $this->InsertTree($tree, $product);
        }

        /**
         * @since 2024-01-06 返回推荐的列表
         */
        $result = [];

        /**
         * @since 2024-01-06 遍历查找的字符串
         */
        $len = strlen($searchWord);
        for($i=0; $i<$len; $i++){
            $w = $searchWord[$i];

            /**
             * @since 2024-01-06 沿着前缀字典树查找
             */
            if(isset($tree->next[$w])){
                $result[] = $tree->next[$w]->words;
                $tree = $tree->next[$w];
            } else {
                $result = array_merge($result, array_fill(0, $len-$i, []));
                break;
            }
        }

        /**
         * @return String[][]
         */
        return $result;
    }

    /**
     * @title word元素插入字典树
     * @author start2004
     * @since 2024-01-06 Created
     *
     * @param WordTree $tree
     * @param String $word
     * @return null
     */
    function InsertTree($tree, $word){
        /**
         * @since 2024-01-06 遍历word字符串
         */
        for($i=0; $i<strlen($word); $i++){
            $w = $word[$i];

            /**
             * @since 2024-01-06 是否存在$w字符
             * @since 2024-01-06 前缀字典树存储word
             */
            if(isset($tree->next[$w])){
                $nextTree = $tree->next[$w];

                /**
                 * @since 2024-01-06 最多3个元素
                 */
                if(count($nextTree->words) < 3){
                    $nextTree->words[] = $word;
                } else {}
            } else {
                $nextTree = new WordTree();
                $tree->next[$w] = $nextTree;
                $nextTree->words[] = $word;
            }

            /**
             * @since 2024-01-06 下次tree
             */
            $tree = $nextTree;
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.06_5.php
 */
$datas = [
    [["mobile","mouse","moneypot","monitor","mousepad"], "mouse"], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->suggestedProducts(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
